using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace Common
{
    public class ImageHelper
    {

        public static byte[] Bitmap2Bytes(Bitmap bmp)
        {
            if (bmp == null) return null;
            try
            {
                Byte[] bytes;

                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                bmp.Save(ms, bmp.RawFormat);
                ms.Position = 0;
                bytes = new byte[ms.Length];
                ms.Read(bytes, 0, bytes.Length);
                ms.Dispose();
                return bytes;
            }
            catch (Exception)
            {
                return null;
            }
        }



        public static Bitmap Bytes2Bitmap(byte[] bytes)
        {
            try
            {
                if (bytes == null) return null;

                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                ms.Write(bytes, 0, bytes.Length);
                Bitmap bmp = new Bitmap(ms);
                return bmp;
            }
            catch (Exception)
            {

                return null;
            }
        }
    }
}
